home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / etc / graph / tick_intrvl.cc < prev    next >
C/C++ Source or Header  |  1994-07-14  |  1KB  |  40 lines

  1. #include "tick_intrvl.h"
  2.  
  3. // TICK_INTERVAL returns the step size which can be used to put a
  4. // specified NO_OF_TICKS beteen the specified UPPER_LIMIT and LOWER_LIMIT.
  5.  
  6. double
  7. tick_interval (double no_of_ticks, double &lower_limit, double &upper_limit)
  8. {
  9.   if (lower_limit == upper_limit)
  10.     {                // make sure the range is nonzero.
  11.       if (lower_limit == 0.)
  12.     {
  13.       lower_limit = -1.;    // this is the traditional behavior of graph.
  14.       upper_limit =  1.;
  15.     }
  16.       else
  17.     {
  18.       lower_limit *= .9;
  19.       upper_limit *= 1.1;
  20.     }
  21.     }
  22.   // compute interval for tick marks.
  23.   double exp = 1.;
  24.   int i = (int) floor (log10 (fabs (upper_limit - lower_limit)) * A_HAIR_MORE);
  25.   for (; 0 < i; i--) exp *= 10.;
  26.   for (; 0 > i; i++) exp /= 10.;
  27.   double mant = (upper_limit - lower_limit) / exp;
  28.   
  29.   double interval = 10.;
  30.   double stepsize = 1.;
  31.   while (interval * (no_of_ticks - 1.) > fabs (mant) * A_HAIR_MORE)
  32.     {
  33.       if (interval - stepsize <= 0.) stepsize /= 10.;
  34.       interval -= stepsize;
  35.     }
  36.   interval *= exp;
  37.   if (mant < 0.) interval = - interval;
  38.   return interval;
  39. }
  40.